
import 'dart:convert';
import 'package:encrypt/encrypt.dart';
///机器码加解密  通过实践判断是否过期
const List<int> bytes = [
  5,3,5,2,5,
  2,3,4,9,4,
  2,5,4,5,2,7];
void main()async{
  String uuid = '3s2e5x15bq21d21bx';

//  String content = '${DateTime.now().millisecondsSinceEpoch}';
//  String encode = EncryptUtil.aesEncode(content,bytes);
//  print(encode);
//  String decode = EncryptUtil.aesDecode(encode,bytes);
//  print(decode);
  String machineCode = base64.encode(uuid.codeUnits).substring(0,16);
  print('机器码===>' + machineCode);
  print(machineCode.codeUnits);

  String jiami = encode(machineCode);
  String jiemi = decode(machineCode,jiami);
  print(jiemi);
}

encode(String machineCode){
  int timeStamp = DateTime.now().millisecondsSinceEpoch;
  print('当前时间 $timeStamp');
  String timeEncode = EncryptUtil.aesEncode("$timeStamp", bytes);
  String result = EncryptUtil.aesEncode(timeEncode, machineCode.codeUnits);
  print(result);
  return result;
}
decode(String machineCode,String d){
  String timeDecode = EncryptUtil.aesDecode(d, machineCode.codeUnits);
  String result = EncryptUtil.aesDecode(timeDecode, bytes);
  return result;
}


class EncryptUtil {

  //aes加密
  static String aesEncode(String content,List<int> bytes) {
    try {
      final key = Key.fromBase64(base64Encode(bytes));
      final encrypter = Encrypter(AES(key, mode: AESMode.cbc));
      final encrypted = encrypter.encrypt(content, iv: IV.fromBase64(base64Encode(bytes)));
      return encrypted.base64;
    } catch (err) {
      print("aes encode error:$err");
      return content;
    }
  }

  //aes解密
  static dynamic aesDecode(dynamic base64,List<int> bytes) {
    try {
      final key = Key.fromBase64(base64Encode(bytes));
      final encrypter = Encrypter(AES(key, mode: AESMode.cbc));
      return encrypter.decrypt64(base64, iv: IV.fromBase64(base64Encode(bytes)));
    } catch (err) {
      print("aes decode error:$err");
      return base64;
    }
  }
}